package com.lzh.o2o.web.shopadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzh.o2o.dto.ProductCategoryExecution;
import com.lzh.o2o.dto.Result;
import com.lzh.o2o.enums.ProductCategoryStateEnum;
import com.lzh.o2o.exception.ProductCategoryOperationException;
import com.lzh.o2o.pojo.ProductCategory;
import com.lzh.o2o.pojo.Shop;
import com.lzh.o2o.service.ProductCategoryService;

/**
 * @author haohao:
 * @date 创建时间：2018年9月13日 下午7:08:23
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
	@Autowired
	private ProductCategoryService productCategoryService;
	Logger logger = LoggerFactory.getLogger(ProductCategoryManagementController.class);

	@RequestMapping(value = "/getproductcategorylist", method = RequestMethod.GET)
	@ResponseBody
	// 通过店铺ID查询对应商品的类别
	private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request) {
		logger.info("查询商铺Id对应的商品类别");
		/*
		 * //To be removed 没有Session 所以没有shopId 都自己默认设置 Shop shop = new Shop();
		 * shop.setShopId(1L); request.getSession().setAttribute("currentShop",
		 * shop);
		 */
		logger.debug("获取当前操作的店铺");
		// 调用service方法
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		List<ProductCategory> list = null;
		if (currentShop != null && currentShop.getShopId() > 0) {
			logger.debug("调用service方法获取当前商店对应商品类别");
			list = productCategoryService.getProductCategoryList(currentShop.getShopId());
			// 返回查询成功的状态码与商品类别数据
			return new Result<List<ProductCategory>>(true, list);
		} else {
			logger.error("调用service方法获取当前商店对应商品类别失败");
			ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
			return new Result<List<ProductCategory>>(false, ps.getState(), ps.getStateInfo());
		}
	}

	@RequestMapping(value = "/addproductcategorys", method = RequestMethod.POST)
	@ResponseBody
	// 批量增加商品的类别
	private Map<String, Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,
			HttpServletRequest request) {
		logger.info("批量增加商品的类别");
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 获取当前操作的店铺
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		for (ProductCategory pc : productCategoryList) {
			pc.setShopId(currentShop.getShopId());
		}
		if (productCategoryList != null && productCategoryList.size() > 0) {
			try {
				logger.debug("调用Service层增加批量增加商品的类别");
				ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
				if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
					logger.debug("批量增加商品类别成功");
					// 添加商品的类别成功之后，Success的状态码为True
					modelMap.put("success", true);
				} else {
					logger.debug("批量增加商品类别失败");
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}

			} catch (ProductCategoryOperationException e) {
				logger.error("批量增加商品异常");
				// 添加商品类别出现异常会状态码为False
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
		} else {
			logger.error("前端为传入商品类别数据");
			// 商品类别数量为空
			modelMap.put("success", false);
			modelMap.put("errMsg", "请至少输出一个商品类别");
		}
		return modelMap;
	}
	
	@RequestMapping(value="/removeproductcategory",method=RequestMethod.POST)
	@ResponseBody
	//删除商品类别
	private Map<String,Object> removeProductCategory(Long productCategoryId,HttpServletRequest request){
		Map<String,Object> modelMap = new HashMap<String,Object>();
		if(productCategoryId !=null &&productCategoryId>0){
			try{
				Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
				ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
				if(pe.getState()==ProductCategoryStateEnum.SUCCESS.getState()){
					modelMap.put("success", true);
				}else{
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			
			}catch(ProductCategoryOperationException e){
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		}else{
			modelMap.put("success", false);
			modelMap.put("errMsg", "请至少选择一个商品类别");

		}
		return modelMap;
	}
}
